package fields;

import model.Player;
import view.Boundary;

public abstract class Ownable extends Field {
	protected Player owner = null;
	protected int price;
	protected boolean isPawned = false;

	public Ownable(int number, String name, int price) {
		super(number, name);
		this.price = price;
		Boundary.setSubText(number, "Pris " + price);
	}
	@Override
	public void landOnField(Player player) {
		landOnField(player, this.owner == null ? 0 : getRent());
	}
	public void landOnField(Player player, int rent) {
		if(this.owner == null && player.calculatePlayerWorth() >= this.price){
			String[] buttons = {"Yes", "No"};
			String selection = Boundary.getPressedButton("You landed on " + getNumber() + " " + getName().replace("-<br>", "") + "." +
				"\nThis lot is for sale.\nDo you wish to buy it for " +
				getPrice() + "?", buttons);
			boolean yesPressed = (selection.equals(buttons[0]));
			if(yesPressed){
				player.withdraw(this.price);
				player.getOwned()[getNumber() - 1] = this;
				setOwner(player);
			}
		}else if(this.owner == null && player.calculatePlayerWorth() < this.price){
			Boundary.showMessage(
				"You landed on " + getNumber() + " " + getName().replace("-<br>", "") + "." + 
				"\nThis lot is for sale, but you cannot afford it.");
		}else if(player.equals(this.owner)){
			Boundary.showMessage("You landed on " + this.getName().replace("-<br>", "") + "," + 
				"\nwhich is owned by you.");
		}else{
			Boundary.showMessage("You landed on " + this.getName().replace("-<br>", "") + "," +
					"\nwhich is owned by "+this.owner.getName()+"\nand " +
					"have to pay " + rent + " in rent.");
			player.transfer(this.owner, rent);
		}
	}
	public abstract int getRent();
	public Player getOwner() {
		return this.owner;
	}
	public void setOwner(Player owner) {
		this.owner = owner;
		Boundary.setOwner(getNumber(), "Owned by: ", owner.getName(), "");
	}
	public int getPrice() {
		return this.price;
	}
	@Override
	public String toString() {
		return super.toString() + " " + "Ownable [" +
				"owner=" + (this.owner == null ? "Bank" : this.owner.getName() + 
						", current rent=" + getRent()) + "]";
	}
	public void setPawned(boolean isPawned) {
		this.isPawned = isPawned;
		Boundary.setSubText(getNumber(), isPawned ? "Pawned" : this.owner.getName());
	}
	public boolean isPawned() {
		return this.isPawned;
	}
}